
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.lxy.service.RecordLogService"%>
<%@page import="org.lxy.domain.RecordLogEntity"%>
<%@page import="org.lxy.service.PointsLogService"%>
<%@page import="org.lxy.domain.PointsLogEntity"%>
<%@page import="org.lxy.service.RecycleGoodsService"%>
<%@page import="org.lxy.domain.RecycleGoodsEntity"%>
<%@page import="org.lxy.service.UserInfoService"%>
<%@page import="org.lxy.domain.UserInfoEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%

	WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(application);
	String id = request.getParameter("id");
	RecordLogService recordLogService = context.getBean(RecordLogService.class);
	PointsLogService pointsLogService = context.getBean(PointsLogService.class);
	RecycleGoodsService recycleGoodsService = context.getBean(RecycleGoodsService.class);
	UserInfoService userInfoService = context.getBean(UserInfoService.class);
	RecycleGoodsEntity entity = recycleGoodsService.getById(Integer.parseInt(id));
	UserInfoEntity userinfo =(UserInfoEntity) request.getSession().getAttribute("Account");
    if(userinfo == null){
    	out.write("0");
    	return;
    }
	if(entity.getChangePoints()>userinfo.getPoints()){
		out.write("2");
		return;
		
	}
	PointsLogEntity jifen= new PointsLogEntity();
	jifen.setPoints(entity.getChangePoints());
	jifen.setUserId(userinfo.getId());
	jifen.setGoodsId(entity.getId());
	System.out.println("兑换记录: "+ JSONObject.toJSONString(jifen));
	pointsLogService.save(jifen);
	RecordLogEntity jilu = new RecordLogEntity();
	jilu.setRecordUser(userinfo.getId());
	Date date = new Date();
	SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");

	//jilu.setCreateTime(date);
	jilu.setGoodsId(entity.getId());
	recordLogService.save(jilu);
	entity.setStatus("已兑换");
	recycleGoodsService.updateById(entity);
	out.write("1");
%>